package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.tour.service.impl.HzyOrdersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteOrder")
public class OrderDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        //创建JSON对象用来返回结果
        JSONObject result = new JSONObject();
        HzyOrdersServiceImpl ordersService = new HzyOrdersServiceImpl();

        //
        String orderIdStr = req.getParameter("orderId");
        String orderType = req.getParameter("orderType");

        //验证参数
        if (orderIdStr == null || orderIdStr.trim().isEmpty() || orderType == null || orderType.trim().isEmpty()) {
            throw new IllegalArgumentException("订单ID或类型不能为空");
        }

        orderIdStr = orderIdStr.replaceAll("^[HST]", "");

        Long orderId = Long.parseLong(orderIdStr);
        boolean success = false;

        // 根据订单类型调用相应的删除方法
        switch (orderType) {
            // 删除酒店订单
            case "酒店":
                success = ordersService.deleteHotelOrder(orderId);
                break;
            // 删除景点订单
            case "景点":
                success = ordersService.deleteScenicOrder(orderId);
                break;
            // 删除门票订单
            case "车票":
                success = ordersService.deleteTicketOrder(orderId);
                break;
        }
        // 返回结果
        result.put("success", success);
        result.put("message", success ? "删除成功" : "删除失败");

        // 返回JSON格式的数据
        resp.getWriter().write(result.toJSONString());
    }
}
